package com.ming.stringstudy.feedbacksystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.stringstudy.feedbacksystem.anno.PermissionLog;
import com.ming.stringstudy.feedbacksystem.entity.Comment;
import com.ming.stringstudy.feedbacksystem.entity.CommentType;
import com.ming.stringstudy.feedbacksystem.pojo.PageBean;
import com.ming.stringstudy.feedbacksystem.entity.UserCommentRelation;
import com.ming.stringstudy.feedbacksystem.mapper.CommentMapper;
import com.ming.stringstudy.feedbacksystem.mapper.CommentTypeMapper;
import com.ming.stringstudy.feedbacksystem.mapper.EvaluationMapper;
import com.ming.stringstudy.feedbacksystem.mapper.UserCommentRelationMapper;
import com.ming.stringstudy.feedbacksystem.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.stringstudy.feedbacksystem.utils.PermissionConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 意见反馈 服务实现类
 * </p>
 *
 * @author aming
 * @since 2024-12-24
 */
@Slf4j
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    public final CommentMapper commentMapper;
    public final UserCommentRelationMapper userCommentRelationMapper;
    public final EvaluationMapper evaluationMapper;
    public final CommentTypeMapper commentTypeMapper;
    // 构造函数注入
    public CommentServiceImpl(CommentMapper commentMapper, UserCommentRelationMapper userCommentRelationMapper, EvaluationMapper evaluationMapper, CommentTypeMapper commentTypeMapper) {
        this.commentMapper = commentMapper;
        this.userCommentRelationMapper = userCommentRelationMapper;
        this.evaluationMapper = evaluationMapper;
        this.commentTypeMapper = commentTypeMapper;
    }

    @PermissionLog(PermissionConstants.COMMENT_WRITE)
    @Transactional(rollbackFor = SQLException.class)
    @Override
    public int insertComment(Comment comment) {
        //判断类型id是否存在
        LambdaQueryWrapper<CommentType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommentType::getId, comment.getCommentTypeId());
        CommentType result = commentTypeMapper.selectOne(queryWrapper);
        if (result != null) {
            //添加问题
            commentMapper.insert(comment);
            log.info(comment.toString());
            //添加用户与问题关系数据，问题者
            userCommentRelationMapper.insert(new UserCommentRelation(comment.getUserId(),comment.getId(),Byte.valueOf("0")));
            return 0;
        }else{
            return -1;
        }
    }
    @PermissionLog(PermissionConstants.REPLY_VIEW)
    @Override
    public PageBean conditionPageList(Comment comment, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        // 生成条件构造器
        LambdaQueryWrapper<Comment> commentWrapper = getCommentLambdaQueryWrapper(comment, begin, end);
        // 设置第几页，一页有几张
        Page<Comment> setPage = new Page<>(page, pageSize);
        // 生成分页数据
        Page<Comment> commentPage = commentMapper.selectPage(setPage, commentWrapper);
        // 获取回复对话列表
        List<Comment> records = commentPage.getRecords();
        // 填充评价内容
        records.forEach(evaluation -> {
            if(evaluation.getEvaluationId() != null){
                evaluation.setEvaluations(evaluationMapper.selectById(evaluation.getEvaluationId()));
            }
        });
        // 获取总长度
        long total = commentPage.getTotal();
        return new PageBean<>(total,records);
    }

    /**
     * 生成最终的条件的构造器
     * @param comment
     * @param begin
     * @param end
     * @return
     */
    private LambdaQueryWrapper<Comment> getCommentLambdaQueryWrapper(Comment comment, LocalDate begin, LocalDate end) {
        // 条件构造器
        LambdaQueryWrapper<Comment> commentWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<UserCommentRelation> userCommentRelationWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<CommentType> commentTypeWrapper = new LambdaQueryWrapper<>();
        // 就行条件判断
        if (comment.getUserId() != null) {
            userCommentRelationWrapper.eq(UserCommentRelation::getUserId, comment.getUserId());
            //获取问题id列表
            List<Integer> list = userCommentRelationMapper.selectList(userCommentRelationWrapper)
                    .stream()
                    .map(UserCommentRelation::getCommentId)
                    .toList();
            commentWrapper.in(Comment::getId,list);
        }
        if (comment.getDeptId() != null) {
            commentTypeWrapper.eq(CommentType::getDeptId, comment.getDeptId());
            List<Integer> list = commentTypeMapper.selectList(commentTypeWrapper)
                    .stream()
                    .map(CommentType::getId)
                    .toList();
            commentWrapper.in(Comment::getCommentTypeId,list);
        }
        if (comment.getCommentTitle() != null) {
            commentWrapper.like(Comment::getCommentTitle, comment.getCommentTitle());
        }
        if (comment.getCommentText() != null) {
            commentWrapper.like(Comment::getCommentText, comment.getCommentText());
        }
        if (comment.getCommentState() != null) {
            commentWrapper.eq(Comment::getCommentState, comment.getCommentState());
        }
        if(comment.getCommentTypeId() != null){
            commentWrapper.eq(Comment::getCommentTypeId, comment.getCommentTypeId());
        }
        if (begin != null && end != null) {
            commentWrapper.between(Comment::getCreateTime, begin, end);
        }
        return commentWrapper;
    }

    @Override
    public void commentReturn(Integer id,String return_reason){
        commentMapper.commentReturn(id,return_reason);
    }
    public void commentRetrieve(Integer id){
        commentMapper.commentRetrieve(id);
    }
    public void commentReassign(Integer id,Integer comment_type_id){
        commentMapper.commentReassign(id, comment_type_id);
    }
    public void commentArchive(Integer id){
        commentMapper.commentArchive(id);
    }

    public List<Comment> selectCommentReturn(){
        return  commentMapper.selectCommentReturn();
    }

    public List<Comment> selectCommentRetrieve(){
        return  commentMapper.selectCommentRetrieve();
    }

    public List<Comment> selectCommentArchive(){
        return  commentMapper.selectCommentArchive();
    }

    public List<Comment> selectCommentReassign(){
        return  commentMapper.selectCommentReassign();
    }

    public int getCommentTypeById(Integer id){
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getId,id);
        Comment comment = commentMapper.selectOne(queryWrapper);
        return comment.getCommentTypeId();
    }
}
